/**
* @Title: HomeController.java
* @Package com.wangwu.week3.controller
* @Description: (描述该文件做什么)
* @author Alex Lu
* @date 2019年9月15日 下午3:39:22
* @version V1.0
*/
package com.wangwu.week3.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bawei.common.utils.StringUtil;
import com.wangwu.week3.domain.Dept;
import com.wangwu.week3.domain.InvestInfo;
import com.wangwu.week3.service.IDeptService;
import com.wangwu.week3.service.IInvestInfoService;
import com.wangwu.week3.vo.InvestInfoVo;

/**
 * @ClassName: HomeController
 * @Description: (描述这个类的作用)
 * @author Alex Lu
 * @date 2019年9月15日 下午3:39:22
 *
 */
@Controller
public class HomeController {
	@Autowired
	private IDeptService deptService;
	
	@Autowired
	private IInvestInfoService investInfoService;
	
	@RequestMapping("/")
	public ModelAndView getInvestInfoList(
			@RequestParam(value="name",required=false)String name,
			@RequestParam(value="minAmount",required=false)String minAmount,
			@RequestParam(value="maxAmount",required=false)String maxAmount,
			@RequestParam(value="pageNo",required=false)Integer pageNo
			){
		ModelAndView mav = new ModelAndView("investInfo-list");
		pageNo = pageNo!= null ?pageNo:1;
		Page<InvestInfoVo> page = new Page<InvestInfoVo>(pageNo,10);
		QueryWrapper<InvestInfoVo> queryWrapper = new QueryWrapper<InvestInfoVo>();
		if(StringUtil.hasText(name)){
			queryWrapper = queryWrapper.like("t_invest_info.name", name);
		}
		if(StringUtil.hasText(minAmount)){
			queryWrapper = queryWrapper.gt("t_invest_info.amount", minAmount);
		}
		if(StringUtil.hasText(maxAmount)){
			queryWrapper = queryWrapper.lt("t_invest_info.amount", maxAmount);
		}
		if(StringUtil.hasText(minAmount) && StringUtil.hasText(maxAmount)){
			queryWrapper = queryWrapper.between("t_invest_info.amount", minAmount,maxAmount);
		}
		IPage<InvestInfoVo> investInfoPages = investInfoService.selectAllInvestInfoByPageAndParam(page, queryWrapper);
		mav.addObject("investInfoPages",investInfoPages);
		mav.addObject("currentPage",investInfoPages.getCurrent());
		mav.addObject("pages",investInfoPages.getPages());
				return mav;
		
	}
	/**
	 * 
	 * @Title: removeMovie
	 * @Description: 批量删除，单条删除，List长度为一就变成单条删除
	 * @param idList
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/remove")
	public ModelAndView removeMovie(@RequestParam("id") List<Integer> idList) {
		ModelAndView mav = new ModelAndView();
		// 批量删除，传一个主键的数组，不需要循环删除
		investInfoService.removeByIds(idList);
		// 删除后停留在本页面
		mav.setViewName("redirect:/");
		return mav;
	}
	/**
	 * 
	 * @Title: show
	 * @Description: 修改页面
	 * @param id
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/showInvest")
	public ModelAndView show(@RequestParam(value = "id", required = true) Integer id) {
		ModelAndView mav = new ModelAndView("updateInvestInfo");
		QueryWrapper<InvestInfoVo> queryWrapper = new QueryWrapper<InvestInfoVo>();
		queryWrapper = queryWrapper.eq("t_invest_info.id", id);
		InvestInfoVo investInfoVo = investInfoService.getOneInvestInfo(queryWrapper);
		List<Dept> depts = deptService.list();
		mav.addObject("investInfo", investInfoVo);
		mav.addObject("depts", depts);
		mav.addObject("id", id);
		return mav;
	}

	/**
	 * 
	 * @Title: updateEngineer
	 * @Description: 修改注册人信息
	 * @param engineer
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/updateInvest")
	public ModelAndView updateEngineer(InvestInfo investInfo) {
		ModelAndView mav = new ModelAndView("redirect:/");
		investInfoService.update(investInfo, new UpdateWrapper<InvestInfo>().eq("id", investInfo.getId()));
		return mav;
	}
	
	@RequestMapping("/addInvest")
	public ModelAndView showAddInvestPage(){
		ModelAndView mav= new ModelAndView("addInvest");
		List<Dept> depts = deptService.list();
		mav.addObject("depts",depts);
		return mav;
	}
	
	@RequestMapping(value="/addInvest",method=RequestMethod.POST)
	public ModelAndView showAddInvestPage(InvestInfo info){
		ModelAndView mav= new ModelAndView("redirect:/");
		investInfoService.saveOrUpdate(info);
		return mav;
	}
	@RequestMapping("/showInvestInfo")
	public ModelAndView showInvestPage(@RequestParam(value = "id", required = true) Integer id){
		ModelAndView mav = new ModelAndView("showInvestInfo");
		QueryWrapper<InvestInfoVo> queryWrapper = new QueryWrapper<InvestInfoVo>();
		queryWrapper = queryWrapper.eq("t_invest_info.id", id);
		InvestInfoVo investInfoVo = investInfoService.getOneInvestInfo(queryWrapper);
		mav.addObject("investInfo", investInfoVo);
		return mav;
	}
}
